<?php
namespace app\api\controller;
use \think\Db;
use \think\Request;
class Index
{
    public function index()
    {

    }

    public function version()
    {
        $get = Request::instance()->get();
        $request = request();
        if(isset($get['id']) and isset($get['version'])){
            $id = $get['id'];
            $version = $get['version'];
            $app = Db::name('app')->where('uid',$id)->find();
            $official_version = $app['official_version'];
            $Debug_version = $app['Debug_version'];
            if(floatval($official_version) == floatval($Debug_version)){
                if(floatval($version) < floatval($official_version)){
                    $local = Db::name('banben')->where(['appid' => $id, 'appversion' => $official_version])->find();
                    if($local['mandatory']=="true"){
                        $forceUpdates = true;
                    }else{
                        $forceUpdates = false;
                    }
                    $pkgUrl = $request->domain().geturl($local['app_download']);
                    $wgtUrl = $request->domain().geturl($local['hot_download']);
                    $shuju = array("needUpdate"=>true,"forceUpdate"=>$forceUpdates,"isTest"=>false,"version"=>$local['appversion'],"description"=>$local['version_release'],"pkgUrl"=>$pkgUrl,"wgtUrl"=>$wgtUrl);
                    $data = array("code"=>0,"data"=>$shuju);
                }elseif (floatval($version) == floatval($official_version)){
                    $local = Db::name('banben')->where(['appid' => $id, 'appversion' => $official_version])->find();
                    $pkgUrl = $request->domain().geturl($local['app_download']);
                    $wgtUrl = $request->domain().geturl($local['hot_download']);
                    $shuju = array("needUpdate"=>false,"forceUpdate"=>false,"isTest"=>false,"version"=>$local['appversion'],"description"=>$local['version_release'],"pkgUrl"=>$pkgUrl,"wgtUrl"=>$wgtUrl);
                    $data = array("code"=>0,"data"=>$shuju);
                }elseif (floatval($version) > floatval($official_version)){
                    $local = Db::name('banben')->where(['appid' => $id, 'appversion' => $official_version])->find();
                    $pkgUrl = $request->domain().geturl($local['app_download']);
                    $wgtUrl = $request->domain().geturl($local['hot_download']);
                    $shuju = array("needUpdate"=>true,"forceUpdate"=>true,"isTest"=>false,"version"=>$local['appversion'],"description"=>$local['version_release'],"pkgUrl"=>$pkgUrl,"wgtUrl"=>$wgtUrl);
                    $data = array("code"=>0,"data"=>$shuju);
                }
            }elseif (floatval($official_version) > floatval($Debug_version)){
                if(floatval($version) < floatval($Debug_version)){
                    $local = Db::name('banben')->where(['appid' => $id, 'appversion' => $official_version])->find();
                    $pkgUrl = $request->domain().geturl($local['app_download']);
                    $wgtUrl = $request->domain().geturl($local['hot_download']);
                    $shuju = array("needUpdate"=>true,"forceUpdate"=>true,"isTest"=>false,"version"=>$local['appversion'],"description"=>$local['version_release'],"pkgUrl"=>$pkgUrl,"wgtUrl"=>$wgtUrl);
                    $data = array("code"=>0,"data"=>$shuju);
                }elseif (floatval($version) == floatval($Debug_version)){
                    $local = Db::name('banben')->where(['appid' => $id, 'appversion' => $Debug_version])->find();
                    $pkgUrl = $request->domain().geturl($local['app_download']);
                    $wgtUrl = $request->domain().geturl($local['hot_download']);
                    $shuju = array("needUpdate"=>false,"forceUpdate"=>false,"isTest"=>false,"version"=>$local['appversion'],"description"=>$local['version_release'],"pkgUrl"=>$pkgUrl,"wgtUrl"=>$wgtUrl);
                    $data = array("code"=>0,"data"=>$shuju);
                }elseif (floatval($version) > floatval($Debug_version)){
                    $local = Db::name('banben')->where(['appid' => $id, 'appversion' => $official_version])->find();
                    $pkgUrl = $request->domain().geturl($local['app_download']);
                    $wgtUrl = $request->domain().geturl($local['hot_download']);
                    $shuju = array("needUpdate"=>false,"forceUpdate"=>false,"isTest"=>false,"version"=>$local['appversion'],"description"=>$local['version_release'],"pkgUrl"=>$pkgUrl,"wgtUrl"=>$wgtUrl);
                    $data = array("code"=>0,"data"=>$shuju);
                }
            }elseif (floatval($official_version) < floatval($Debug_version)){
                if(floatval($version) < floatval($Debug_version) && floatval($version) == floatval($official_version)){
                    $local = Db::name('banben')->where(['appid' => $id, 'appversion' => $official_version])->find();
                    $pkgUrl = $request->domain().geturl($local['app_download']);
                    $wgtUrl = $request->domain().geturl($local['hot_download']);
                    $shuju = array("needUpdate"=>false,"forceUpdate"=>false,"isTest"=>false,"version"=>$local['appversion'],"description"=>$local['version_release'],"pkgUrl"=>$pkgUrl,"wgtUrl"=>$wgtUrl);
                    $data = array("code"=>0,"data"=>$shuju);
                }elseif (floatval($version) < floatval($Debug_version) && floatval($version) < floatval($official_version)){
                    $local = Db::name('banben')->where(['appid' => $id, 'appversion' => $official_version])->find();
                    $pkgUrl = $request->domain().geturl($local['app_download']);
                    $wgtUrl = $request->domain().geturl($local['hot_download']);
                    $shuju = array("needUpdate"=>true,"forceUpdate"=>false,"isTest"=>false,"version"=>$local['appversion'],"description"=>$local['version_release'],"pkgUrl"=>$pkgUrl,"wgtUrl"=>$wgtUrl);
                    $data = array("code"=>0,"data"=>$shuju);
                }elseif (floatval($version) == floatval($Debug_version) && floatval($version) > floatval($official_version)){
                    $local = Db::name('banben')->where(['appid' => $id, 'appversion' => $Debug_version])->find();
                    $pkgUrl = $request->domain().geturl($local['app_download']);
                    $wgtUrl = $request->domain().geturl($local['hot_download']);
                    $shuju = array("needUpdate"=>false,"forceUpdate"=>false,"isTest"=>false,"version"=>$local['appversion'],"description"=>$local['version_release'],"pkgUrl"=>$pkgUrl,"wgtUrl"=>$wgtUrl);
                    $data = array("code"=>0,"data"=>$shuju);
                }elseif (floatval($version) > floatval($Debug_version) && floatval($version) > floatval($official_version)){
                    $local = Db::name('banben')->where(['appid' => $id, 'appversion' => $official_version])->find();
                    $pkgUrl = $request->domain().geturl($local['app_download']);
                    $wgtUrl = $request->domain().geturl($local['hot_download']);
                    $shuju = array("needUpdate"=>false,"forceUpdate"=>false,"isTest"=>false,"version"=>$local['appversion'],"description"=>$local['version_release'],"pkgUrl"=>$pkgUrl,"wgtUrl"=>$wgtUrl);
                    $data = array("code"=>0,"data"=>$shuju);
                }
            }
        }else{
            $data = array("code"=>-1,"msg"=>"必传参数为空");
        }
        return json_encode($data,JSON_UNESCAPED_UNICODE);
    }
}
